package cn.wangdian.erp.demo;

import cn.wangdian.erp.sdk.Client;
import cn.wangdian.erp.sdk.WdtErpException;
import cn.wangdian.erp.sdk.api.purchasereturn.PurchaseReturnAPI;
import cn.wangdian.erp.sdk.impl.ApiFactory;
import cn.wangdian.erp.sdk.impl.DefaultClient;
import cn.wangdian.erp.sdk.api.purchasereturn.dto.PurchaseReturnCancelBatchResponse;
import cn.wangdian.erp.sdk.api.purchasereturn.dto.PurchaseReturnCancelResponse;
import cn.wangdian.erp.sdk.api.purchasereturn.dto.PurchaseReturnCreateOrderRequest;
import cn.wangdian.erp.sdk.api.purchasereturn.dto.PurchaseReturnCreateOrderResponse;
import cn.wangdian.erp.sdk.api.purchasereturn.dto.PurchaseReturnPendingResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PurchaseReturn
{

	public static void main(String[] args) throws WdtErpException
	{
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");

		PurchaseReturnAPI purchaseReturnAPI = ApiFactory.get(client, PurchaseReturnAPI.class);

//		createOrderTest(purchaseReturnAPI);
//		cancelOrderTest(purchaseReturnAPI);
//		cancelBatchTest(purchaseReturnAPI);
		pendingTest(purchaseReturnAPI);
	}

	private static void createOrderTest(PurchaseReturnAPI purchaseReturnAPI)
	{
		PurchaseReturnCreateOrderRequest.PurchaseReturnOrderInfo orderInfo = new PurchaseReturnCreateOrderRequest.PurchaseReturnOrderInfo();
		orderInfo.setOuterNo("CxxR201501010004");
		orderInfo.setWarehouseNo("lj_test3");
		orderInfo.setProviderNo("1001");
		orderInfo.setPostFee(new BigDecimal(5));
		orderInfo.setOtherFee(new BigDecimal(10));
		orderInfo.setRemark("lichTest");
		orderInfo.setProvince(110000);
		orderInfo.setCity(110100);
		orderInfo.setDistrict(110103);

		PurchaseReturnCreateOrderRequest.PurchaseReturnDetail detail = new PurchaseReturnCreateOrderRequest.PurchaseReturnDetail();
		detail.setSpecNo("4565656565");
		detail.setDiscount(new BigDecimal(5));
		detail.setNum(new BigDecimal(5));
		detail.setPrice(new BigDecimal(5));
		detail.setRemark("test");

		List<PurchaseReturnCreateOrderRequest.PurchaseReturnDetail> detailList = new ArrayList<>();
		detailList.add(detail);
		boolean isSubmit = false;

		PurchaseReturnCreateOrderResponse response = purchaseReturnAPI.createOrder(orderInfo, detailList, isSubmit);
		System.out.println(response.getMessage());
	}

	private static void cancelOrderTest(PurchaseReturnAPI purchaseReturnAPI) throws WdtErpException
	{
		String returnNo = "CR202305260001";

		PurchaseReturnCancelResponse response = purchaseReturnAPI.cancel(returnNo);
		System.out.println(response.getStatus());
		System.out.println(response.getMessage());
	}

	private static void cancelBatchTest(PurchaseReturnAPI api) throws WdtErpException
	{
		List<String> returnNoList = new ArrayList<>();
		returnNoList.add("CR202304260002");

		PurchaseReturnCancelBatchResponse response = api.cancelBatch(returnNoList);

		if (response.getErrorNum() > 0)
		{
			List<PurchaseReturnCancelBatchResponse.ErrorInfo> errorInfo = response.getErrorInfo();
			System.out.println("错误数量: "+response.getErrorNum());
			for (PurchaseReturnCancelBatchResponse.ErrorInfo info : errorInfo)
			{
				System.out.println("单号:"+info.getOrderNo()+","+info.getMessage());
			}
		}
		else
		{
			System.out.println("success");
		}
	}

	private static void pendingTest(PurchaseReturnAPI api) throws WdtErpException
	{
		List<String> purchaseReturnNoList = new ArrayList<>();
		purchaseReturnNoList.add("CR202301290001");

		PurchaseReturnPendingResponse response = api.pending(purchaseReturnNoList);
		List<PurchaseReturnPendingResponse.ErrorInfo> errorInfoList = response.getErrorInfoList();
		if (!errorInfoList.isEmpty())
		{
			for (PurchaseReturnPendingResponse.ErrorInfo errorInfo : errorInfoList)
			{
				System.out.println("异常,单号: "+errorInfo.getOrderNo()+","+errorInfo.getMessage());
			}
		}
		else
		{
			System.out.println("success");
		}
	}
}
